﻿using GalaSoft.MvvmLight;
using GalaSoft.MvvmLight.Command;
using Newtonsoft.Json;
using Power.Models;
using Power.View;
using Power.View.Dialog;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net;
using System.Text;
using System.Threading.Tasks;
using System.Web;

namespace Power.ViewModel
{
    public class ReportGenerationViewModel: ViewModelBase
    {
        private ReportGeneration model;
        public ReportGenerationViewModel(ReportGeneration model)
        {
            this.model = model;
            Cancel = new RelayCommand(OnCancel);
            Submit = new RelayCommand(OnSubmit);
            Data = new ReportGenerationEntity
            {
                BatchSelectList = Common.Common.GetBatchSelectList(),
                ReportName = "text",
                ProjectName = "text",
                ProjectNo = "text",
                ProjectLocation = "text",
                DetectionUnit = "text",
                TestDateS = DateTime.Now.ToString("yyyy/MM/dd"),
                TestDateE = DateTime.Now.ToString("yyyy/MM/dd"),
                ReportNo = "text",
                TestingPersonnel = "text",
                ReportPreparation= "text",
                Check = "text",
                ToExamine = "text",
                Approval= "text",
                AddressOfTestingUnit= "text",
                ZipCodeOfTestingUnit= "text",
                TelephoneOfTestingUnit= "text",
                ContactPersonOfTestingUnit= "text",
                ConstructionUnit= "text",
                DesignUnit= "text",
                ConstructionEnterprise= "text",
                ConstructionControlUnit= "text",
                EntrustedUnit= "text",
                DetectionPurpose= "text",
                TestingInstrumentsAndEquipment= "text",
                DeviceMovementMode= "text",
                PipelinePluggingMethod= "text",
                TemporaryDrainageMethod= "text",
                PipelineCleaningMethod= "text"
            };
        }
        private Tuple<bool,string> Check()
        {
            if (string.IsNullOrEmpty(Data.ReportName))
                return new Tuple<bool, string>(false,"报告名称不能为空");
            if (string.IsNullOrEmpty(Data.ProjectName))
                return new Tuple<bool, string>(false, "工程名称不能为空");
            if (string.IsNullOrEmpty(Data.ProjectNo))
                return new Tuple<bool, string>(false, "工程编号不能为空");
            if (string.IsNullOrEmpty(Data.ProjectLocation))
                return new Tuple<bool, string>(false, "工程地点不能为空");
            if (string.IsNullOrEmpty(Data.DetectionUnit))
                return new Tuple<bool, string>(false, "检测单位不能为空");
            if (string.IsNullOrEmpty(Data.TestDateS) || string.IsNullOrEmpty(Data.TestDateE))
                return new Tuple<bool, string>(false, "检测日期不能为空");
            if (string.IsNullOrEmpty(Data.ReportNo))
                return new Tuple<bool, string>(false, "报告编号不能为空");
            if (string.IsNullOrEmpty(Data.TestingPersonnel))
                return new Tuple<bool, string>(false, "检测人员不能为空");
            if (string.IsNullOrEmpty(Data.ReportPreparation))
                return new Tuple<bool, string>(false, "报告编写不能为空");
            if (string.IsNullOrEmpty(Data.Check))
                return new Tuple<bool, string>(false, "校核不能为空");
            if (string.IsNullOrEmpty(Data.ToExamine))
                return new Tuple<bool, string>(false, "审核不能为空");
            if (string.IsNullOrEmpty(Data.Approval))
                return new Tuple<bool, string>(false, "批准不能为空");
            if (string.IsNullOrEmpty(Data.AddressOfTestingUnit))
                return new Tuple<bool, string>(false, "检测单位地址不能为空");
            if (string.IsNullOrEmpty(Data.ZipCodeOfTestingUnit))
                return new Tuple<bool, string>(false, "检测单位邮编不能为空");

            if (string.IsNullOrEmpty(Data.TelephoneOfTestingUnit))
                return new Tuple<bool, string>(false, "检测单位电话不能为空");

            if (string.IsNullOrEmpty(Data.ContactPersonOfTestingUnit))
                return new Tuple<bool, string>(false, "检测单位联系人不能为空");


            if (string.IsNullOrEmpty(Data.ConstructionUnit))
                return new Tuple<bool, string>(false, "建设单位不能为空");
            if (string.IsNullOrEmpty(Data.DesignUnit))
                return new Tuple<bool, string>(false, "设计单位不能为空");

            if (string.IsNullOrEmpty(Data.ConstructionEnterprise))
                return new Tuple<bool, string>(false, "施工单位不能为空");
            if (string.IsNullOrEmpty(Data.ConstructionControlUnit)) 
                return new Tuple<bool, string>(false, "监理单位不能为空");
            if (string.IsNullOrEmpty(Data.EntrustedUnit))
                return new Tuple<bool, string>(false, "委托单位不能为空");
            if (string.IsNullOrEmpty(Data.DetectionPurpose))
                return new Tuple<bool, string>(false, "检测目的不能为空");
            if (string.IsNullOrEmpty(Data.TestingInstrumentsAndEquipment))
                return new Tuple<bool, string>(false, "检测仪器设备不能为空");
            if (string.IsNullOrEmpty(Data.DeviceMovementMode))
                return new Tuple<bool, string>(false, "设备移动方式不能为空");
            if (string.IsNullOrEmpty(Data.PipelinePluggingMethod))
                return new Tuple<bool, string>(false, "管道封堵方法不能为空");
            if (string.IsNullOrEmpty(Data.TemporaryDrainageMethod))
                return new Tuple<bool, string>(false, "临时排水方法不能为空");
            if (string.IsNullOrEmpty(Data.PipelineCleaningMethod))
                return new Tuple<bool, string>(false, "管道清洗方法不能为空");

            return new Tuple<bool, string>(true, "");
        }
        private void OnSubmit()
        {
            var ret = Check();
            if (ret.Item1 == false)
            {
                MessagePrompt box = new MessagePrompt(ret.Item2);
                box.ShowDialog();
                return;
            }
            string file = GetFileResult();
            System.Windows.Forms.FolderBrowserDialog folderBrowserDialog = new System.Windows.Forms.FolderBrowserDialog();
            var result = folderBrowserDialog.ShowDialog();
            if (!string.IsNullOrWhiteSpace(file) && (result == System.Windows.Forms.DialogResult.OK || result == System.Windows.Forms.DialogResult.Yes))
            {
                //获取文件名称
                var fileName = HttpUtility.UrlDecode(new Uri(file).Segments.Last());
                //下载文件
                WebClient myWebClient = new WebClient();
                myWebClient.DownloadFile(file, $"{folderBrowserDialog.SelectedPath}\\{fileName}");
                MessagePrompt box1 = new MessagePrompt("下载完成");
                box1.ShowDialog();
                return;
            }
        }
        private string GetFileResult()
        {
            //拼接JSON串
            var datas = JsonConvert.SerializeObject(this.Data);
            //请求接口(管网人员实现)

            //返回文件地址
            return "http://120.77.177.121/test.xlsx"; 
        }

        private void OnCancel()
        {

        }

        #region 

        public RelayCommand Cancel { get; private set; }

        public RelayCommand Submit {  get; private set; }

        public ReportGenerationEntity Data { get; private set; }
        #endregion
    }
}
